package com.springboot.study.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* 完整可运行的 Spring Boot + RabbitMQ Direct Exchange 日志消息案例，包括 配置类、生产者、消费者、启动类和测试接口，并附带注释。
* */
@Configuration
public class DirectConfig {

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String ERROR_QUEUE = "error.queue";
    public static final String INFO_QUEUE = "info.queue";
    public static final String WARNING_QUEUE = "warning.queue";

    // 声明 Direct 类型交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    // 声明队列
    @Bean
    public Queue errorQueue() {
        return QueueBuilder.durable(ERROR_QUEUE).build();
    }

    @Bean
    public Queue infoQueue() {
        return QueueBuilder.durable(INFO_QUEUE).build();
    }

    @Bean
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }

    // 绑定队列到交换机，指定路由键
    @Bean
    public Binding errorBinding() {
        return BindingBuilder.bind(errorQueue()).to(directExchange()).with("error");
    }

    @Bean
    public Binding infoBinding() {
        return BindingBuilder.bind(infoQueue()).to(directExchange()).with("info");
    }

    @Bean
    public Binding warningBinding() {
        return BindingBuilder.bind(warningQueue()).to(directExchange()).with("warning");
    }
}
